import { defineStore } from 'pinia'
import {
  bannerList, newsList, hotCourseList, hotTeacherList

} from '/@/api/homePage/index'
import { BannerInfo, courseInfo, newsInfo, TeacherInfo } from './types'

export const useHomePageState = defineStore('homePage', {
  actions: {

    // 异步调用接口获取学生首页banner
    async getBanners(data :any): Promise<BannerInfo[]> {

      let banners:BannerInfo[] = [];
         await bannerList(data).then((res) => {
          //console.log(res);
          const { bannerInfos } = res;
          //console.log(bannerInfos);
          banners = bannerInfos;
        });

      //console.log(banners);
      return banners;
    },
    async getNews (data: any){
      let total : number = 0;
      let newsInfos : newsInfo[] = [];
      await newsList(data).then((res) => {

        //console.log(res);
        //const {informationInfos} = res;
        //console.log(informationInfos);
        total = res.total;
        newsInfos = res.informationInfos;
      });

      //console.log(newsInfos);
      //return {total, newsInfos};
      return newsInfos;
    },
    async getHotCourses (data:any):Promise<courseInfo[]>{

      let hotCourses : courseInfo[] = [];
      await hotCourseList(data).then((res) => {
        //console.log(res);
        const { courseInfos}  = res;
        //console.log(courseInfos);
        hotCourses = courseInfos;
      });
      return hotCourses;
    },
    async getHotTeachers(data:any):Promise<TeacherInfo[]>{ // 学生首页热门教师
      let hotTeachers :TeacherInfo[] = [];

      await hotTeacherList(data).then((res) => {
        const { teacherInfos } = res;
        hotTeachers = teacherInfos;
      })
      return hotTeachers;
    }
  },
})
